package cate.game.server.controller;

import cate.game.GameBody;
import cate.game.ManyGame;
import cate.game.mail.po.MailBuilder;
import cate.game.role.mail.Mail;
import cp.platform.msg.MsgPushInput;
import cp.platform.msg.MsgPushOutput;
import easy.java.spring.annotation.MyPostMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@RequestMapping(PlatformSupportController.PATH_BASE)
@Controller
public class PlatformSupportController extends GameSrvBaseController {

    public static final String PATH_BASE = "platform";
    public static final String PATH_MSG_PUSH = "/msg/push";

    public static final String getPath(String subPath) {
        return PATH_BASE + subPath;
    }

    private ManyGame games;
    @Autowired
    public void setGames(ManyGame games) {
        this.games = games;
    }

    @MyPostMapping(PATH_MSG_PUSH)
    @ResponseBody
    public MsgPushOutput onMsgPush(@RequestBody MsgPushInput input) {
        log.info("收到消息推送的请求：{}", input);
        MsgPushOutput output = new MsgPushOutput();
        try {
            GameBody game = games.get(input.toSrvId());
            if(game != null) {
                Mail mail = new MailBuilder().setTitle(input.getTitle()).setContent(input.getContent()).build();
                game.mail.send(input.getRoleId(), mail);
                output.success("邮件发送成功", mail.getUid());
            } else {
                output.fail("找不到ID="+input.getGsId()+"的游戏服");
            }
        } catch (Exception e) {
            log.error("", e);
            output.fail(e);
        }
        return output;
    }
}
